import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

public class Coll extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/jsp;charset=utf-8");
        String username1 = req.getParameter("username").trim();
        String password2 = req.getParameter("password").trim();
        System.out.println(username1+","+password2);
        Connection conn = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String username="root";
            String password="123456";
            String url="jdbc:mysql://localhost:3306/myuser?serverTimezone=GMT";
            conn = DriverManager.getConnection(url,username,password);
            String sql = "SELECT * FROM USER WHERE NAME=? AND PASSWORD=?";
            pre = conn.prepareStatement(sql);
            System.out.println("张三");
            pre.setObject(1,username1);
            pre.setObject(2,password2);
            res = pre.executeQuery();
            if (res.next()){
                String name1 = res.getString("name");
                req.setAttribute("name",name1);
                System.out.println(name1);
                String password1 = res.getString("password");
                //req.setAttribute("password",password1);
                String sex = res.getString("sex");
                System.out.println(sex);
                req.setAttribute("sex",sex);
                System.out.println(password1);
                req.getRequestDispatcher("/welcome.jsp").forward(req,resp);
            }else{
                req.getRequestDispatcher("/error.jsp").forward(req,resp);
            }

        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            System.out.println(conn);
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (pre!=null){
                try {
                    pre.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (res!=null){
                try {
                    res.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }


       /* String username1 = req.getParameter("username");
        String password2 = req.getParameter("password");
        System.out.println(username1+","+password2);
        Connection conn = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String username="root";
            String password="123456";
            String url="jdbc:mysql://localhost:3306/myuser?serverTimezone=GMT";
            conn = DriverManager.getConnection(url,username,password);
            String sql = "SELECT * FROM USER WHERE NAME=? AND PASSWORD=?";
            pre = conn.prepareStatement(sql);
            System.out.println("张三");
            pre.setObject(1,username1);
            pre.setObject(2,password2);
            res = pre.executeQuery();
            if (res.next()){
                String name1 = res.getString("name");
                req.setAttribute("name",name1);
                System.out.println(name1);
                String password1 = res.getString("password");
                //req.setAttribute("password",password1);
                String sex = res.getString("sex");
                System.out.println(sex);
                req.setAttribute("sex",sex);
                System.out.println(password1);
                req.getRequestDispatcher("/welcome.jsp").forward(req,resp);
            }else{
                req.getRequestDispatcher("/error.jsp").forward(req,resp);
            }

        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            System.out.println(conn);
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (pre!=null){
                try {
                    pre.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (res!=null){
                try {
                    res.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }*/
    }


}}
